/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-04-26 08:41:18
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-26 13:54:05
 * @FilePath: /yt-devops/lib/pages/private_policy/private_policy_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/cupertino.dart';
// import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/utils/func_utils.dart';

class PrivatePolicyController extends GetxController {
  late WebViewController webViewController;
  // String htmlString = 'http://ytoss.yotaienergy.com:8989/zh/PrivacyPolicy.html';
  String htmlString = 'http://192.168.3.18:8989/en/PrivacyPolicy.html';
  bool isLoad = false;
  @override
  void onInit() {
    super.onInit();

    Log.d('==Configs.enviromentModel.api===${Configs.enviromentModel.api}');
    String? languageCode = Get.locale?.languageCode;
    if (languageCode != null && languageCode.toString() == 'en') {
      htmlString = '${Configs.enviromentModel.api}:8989/en/PrivacyPolicy.html';
    } else {
      htmlString = '${Configs.enviromentModel.api}:8989/zh/PrivacyPolicy.html';
    }
    htmlString = htmlString.replaceAll('https', 'http');
    Log.d('==htmlString===' + htmlString.toString());
    // webViewController = WebViewController()..loadRequest(Uri.parse(htmlString));
    webViewController = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            Log.d('onPageFinished===' + url.toString());
            if(isLoad == false){
              webViewController.reload();
            }
            isLoad = true;
          },
          onWebResourceError: (WebResourceError error) {
            Log.d('WebResourceError==' + error.toString());
          },
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(htmlString));
  }

  @override
  void onReady() {}

  @override
  void onClose() {}
}
